/*******************************************************************************
 * Copyright (c) 2011 Prometheus Consulting
 *
 *     Licensed under the Apache License, Version 2.0 (the "License"); you may
 *     not use this file except in compliance with the License. You may obtain
 *     a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *     License for the specific language governing permissions and limitations
 *     under the License.
 *******************************************************************************/
/**
 * 
 */
package nz.co.senanque.vaadinsupport.viewmanager;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import nz.co.senanque.vaadinsupport.permissionmanager.PermissionManagerImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.LoginForm.LoginListener;

/**
 * This uses the Spring Authentication mechanism to authenticate the user and load the permissions.
 * 
 * @author Roger Parkinson
 *
 */
public class SpringLoginListener implements LoginListener,TouchLoginListener {
	
	private static final long serialVersionUID = 4784656236097876308L;
	@Autowired private PermissionManagerImpl m_permissionManager;
	@Autowired private AuthenticationManager m_authenticationManager;

	public void onLogin(LoginEvent event) {
		String username = event.getLoginParameter("username");
		String password = event.getLoginParameter("password");
		login(username,password);
	}
	
	public void onLogin(Map<String, String> map) {
		String username = map.get("user");
		String password = map.get("password");
		login(username,password);
	}

	private void login(String username, String password)
	{
		Set<String> permissionsList = new HashSet<String>();
		try {
			Authentication authentication = new UsernamePasswordAuthenticationToken(username,password);
			authentication = getAuthenticationManager().authenticate(authentication);
			for (GrantedAuthority grantedAuthority : authentication.getAuthorities())
			{
				permissionsList.add(grantedAuthority.getAuthority());
			}
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		getPermissionManager().setPermissionsList(permissionsList);
		getPermissionManager().setCurrentUser(username);
	}

	public PermissionManagerImpl getPermissionManager() {
		return m_permissionManager;
	}

	public void setPermissionManager(PermissionManagerImpl permissionManager) {
		m_permissionManager = permissionManager;
	}

	public AuthenticationManager getAuthenticationManager() {
		return m_authenticationManager;
	}

	public void setAuthenticationManager(AuthenticationManager authenticationManager) {
		m_authenticationManager = authenticationManager;
	}

}
